(function(window,undefined){
	var Search = {};
	window.Search = window.Search || Search;
	Search.searchBlock = function(){
		UIAssist.searchBoxBlock();
		
		$('#topSearch button').off('click').on("click",function(event){
			event.preventDefault();
			
			var _keyword = $('#searchWord').val();
			if(!_keyword){
				alert('검색어를 입력하여 주세요.');
				return;
			}
			sessionStorage.setItem('keyword',_keyword);
			location.href='search_result.php';
			
		});
	};
	
	Search.searchResultBlock = function(){
		searchList('만족도 순');
		$('div.storeHead > ul > li > a:nth(0)').addClass('orange');
		var _sorting;
		
		$('div.storeHead > ul > li > a').off('click').on("click",function(event){
			event.preventDefault();
			Paging.initCurrentPage();
			_sorting=$(this).html();
			$('div.storeHead > ul > li > a').removeClass('orange');
			$(this).addClass('orange');
			searchList(_sorting);
			
		});
	};
	
	var searchList = function(_sorting){
		searchStore (_sorting,function(result){
			var selector = 'ul.storeList';
			$(selector).empty();
		
			if(result.dataLength == 0){		
				var landmark_name=$('#selectLoc a').html();	
				
				template.noStore(selector,landmark_name);
				$('.left .unitWrap.styleA').css("border","none"); 
				
			}
					
			$.each(result.data,function(key){
				$('.left .unitWrap.styleA').css("border","1px solid #ccc"); 
				if(this.store_image==false){
					this.store_image='images/default_store.png';
				}
						
				if(this.isCoupon==true){
					this.name=this.name+' <img src="images/icon_coupon.png" alt="쿠폰">';
				}
						
				template.storeList(this,selector,key);
						
				Store.loadFavoriteUser(this.storeFavoriteUser,key,this.isFavorite);
				Store.loadPost(this.storePost,key,'store');
			});
			
			Store.store_listener();
			$('.storeHead span').html('<b class="orange">'+result.dataLength+'</b><b class="cnt"> 개 </b> ('+_sorting+')');

		});
	};
	var searchStore = function(_sorting,_callback){
		var _user_id= sessionStorage.getItem('user_id');
		var _keyword = sessionStorage.getItem('keyword');
		var _landmark_code = sessionStorage.getItem('landmark_code');
		var _category= sessionStorage.getItem('category_code');
		$('.centerContent h2').html('<div class="searchTitle"><span>'+sessionStorage.getItem('landmark_name')+'</span> 근처의 <span>'+_keyword+'</span>에 대한 검색결과입니다.</div>');
		$.ajax({
			type:"post",
			url:ajaxURL+"search.php?action=searchStore",
			data:{user_id:_user_id,sorting:_sorting,landmark_code:_landmark_code,category:_category,keyword:_keyword,row:Paging.getCurrent()},
			dataType:"json",
			success:function(result){
				_callback(result);
								var _para = {
					dataLength : result.dataLength,
					// dataLength : 1000,
					selector : '#pagination > ul',
					callback : function(){
						Store.storeList();
					}
				};
						
				Paging.innerAJAX(_para);
			}
		});
	};
	
	// Search.searchPost = function(){
		// var _landmark = "A00005";
		// var _keyword = "h";
// 		
		// $.ajax({
			// type:"post",
			// url:ajaxURL + "search.php?action=searchPost",
			// data:{landmark:_landmark,keyword:_keyword},
			// dataType:"json",
			// success:function(result){
			// }
		// });
	// };
	
	var template = {
		noStore : function(selector,landmark_name){
			
			var info='';
			info+='<div class="noneListWrap">';
    		info+='<div class="titleArea">';
	    	info+='<img src="images/nolist_store.png" alt="nolist_store"  />';
    		info+='<h4><span>'+sessionStorage.getItem('landmark_name')+'</span> 근처의 <span>'+sessionStorage.getItem('keyword')+'</span>에 대한 검색결과가 없습니다.<br/>다른 검색어로 검색하시거나, 추천 스토어를 이용해보세요.</h4>'
    		info+='</div>';
    		info+='<div class="subArea">';
    		info+='<p>고객님이 알고 계시는 상점을 추천해 주세요.<br/>아니면 사장님이실 경우 가게를 등록해 주세요.<br/>가맹문의 : 02) 155 - 2276<br/><br/>';				
			info+='<a href="#"><img src="images/btn_reg.png" alt="등록하기"/></a>';
    		info+='</p></div> </div>';
    		$(selector).append(info);
		},
			
		storeList : function(result,selector,key){
			var info = '';
			if(key == 0){
				info += '<li class="first-child">';
			} else {
				info += '<li>';
			}
            info += '<div class="thumbWrap">';
            info += '<a data-store_id="'+result.store_id+'" href="store_view.php?store_id='+result.store_id+'">';
            info += '<img src="'+result.store_image+'" width="160px" height="120px" alt="thumb_store_etc">';
            info += '</a>';    
            info += '</div>';  
            if(key==0){   
            	info += '<span class="ranking rank1">'+(key+1)+'</span>';  
            }else{
            	info += '<span class="ranking">'+(key+1)+'</span>'; 
            }                                                        
            info += '<div class="infoWrap">';                     
            info += '<a data-store_id="'+result.store_id+'" class="title" href="store_view.php?store_id='+result.store_id+'">'+result.name+'</a>';                         
            info += '<div class="tags">'+result.category_name+' <span>|</span> '+result.addr2+'</div>';                     
            info += '<div class="values">';
            info += '만족도 <b>'+result.satisfaction+'%</b>&nbsp;&nbsp;&nbsp;&nbsp;리뷰 <b>'+result.talk_count+'</b>개';
            info += '</div>';                         
            info += '<div id="friends" data-store_id="'+result.store_id+'" class="friends">';
            info += '</div>';                         
            info += '<div class="favorites">';
           // info += '<span class="weekly"> <b>310</b>명</span><br>';
            info += '<span class="total"> <b>'+result.favorite_count+'</b>명</span><br>';
            info += '</div>';                      
            info += '</div>';                              
            info += '<div class="commentWrap">';
           	info += '</div></li>';
	
			$(selector).append(info);
		}
	};
})(window);
